<template>
  <div ref="chartRef" :style="{ width, height }"></div>
</template>
<script lang="ts" setup>
  import { Ref, ref, watch } from 'vue';

  import { useECharts } from '/@/hooks/web/useECharts';

  const props = defineProps({
    loading: Boolean,
    width: {
      type: String as PropType<string>,
      default: '100%',
    },
    height: {
      type: String as PropType<string>,
      default: '300px',
    },

    seriesVal: {
      type: Object,
      default: () => {},
    },
  });

  const chartRef = ref<HTMLDivElement | null>(null);
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);

  watch(
    () => props.loading,
    () => {
      if (props.loading) {
        return;
      }
      if (props.seriesVal.series.length === 0) {
        setOptions({
          title: {
            textStyle: {
              color: 'grey',
              fontSize: 20,
            },
            text: '暂无数据',
            left: 'center',
            top: 'center',
          },
        });
      } else {
        setOptions({
          tooltip: {
            trigger: 'axis',
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true,
          },
          xAxis: {
            type: 'category',
            boundaryGap: false,
            data: [],
          },
          yAxis: {
            type: 'value',
            name: '',
          },
          series: [],
          ...props.seriesVal,
        });
      }
    },
    { immediate: true },
  );
</script>
